#include <gtk/gtk.h>
#include <stdlib.h>
#include <stdbool.h>

static const char *peoples[] = {
	"Tom",
	"Jakie Chen",
	"Lucy Liu",
	"Amy Smart",
	"Robet Zhang",
	"Bill Joy"
};

static const char *weathers[] = {
	"Sunshine",
	"Cloudy",
	"Rainy",
	"Snow",
	"Foggy"
};

static const char *feelings[] = {
	"Very Good",
	"Good",
	"Normal",
	"Bad",
	"Very Bad"
};

#define PCHAR_SIZE (sizeof(char*))

void button_add_clicked(gpointer data){
	int rp,rw,rf;
	rp = random()%(sizeof(peoples)/PCHAR_SIZE);
	rw = random()%(sizeof(weathers)/PCHAR_SIZE);
	rf = random()%(sizeof(feelings)/PCHAR_SIZE);
	printf("%s:people weather feeling is %d %d %d\n",__func__,rp,rw,rf);
	const char *one_row[] = {peoples[rp],weathers[rw],feelings[rf]};
	gint row = gtk_clist_append(GTK_CLIST(data),(gchar **)one_row);
	gtk_clist_moveto(GTK_CLIST(data),row,0,0,0);
}

void button_clear_clicked(gpointer data){
	gtk_clist_clear(GTK_CLIST(data));
}

void button_hide_show_clicked(gpointer data){
	static bool flag = false;
	if(!flag)
		gtk_clist_column_titles_hide(GTK_CLIST(data));
	else
		gtk_clist_column_titles_show(GTK_CLIST(data));
	flag = !flag;
}

void selection_made(GtkWidget *clist,gint row,gint col,GdkEventButton *event,gpointer data){
	gchar *text;
	gtk_clist_get_text(GTK_CLIST(clist),row,col,&text);
	printf("%s:row %d col %d text is %s\n",__func__,row,col,text);
}

int main(int argc,char **argv){
	GtkWidget *w,*vbox,*hbox,*scrolled_w,*clist;
	GtkWidget *btn_add,*btn_clear,*btn_hide_show;
	gchar *titles[3] = {"People","Weather","Feeling"};

	srandom(time(NULL));
	gtk_init(&argc,&argv);
	w = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_widget_set_usize(w,400,250);
	gtk_window_set_title(GTK_WINDOW(w),"GtkCList Example");
	gtk_signal_connect(GTK_OBJECT(w),"destroy",G_CALLBACK(gtk_main_quit),NULL);

	vbox = gtk_vbox_new(FALSE,5);
	gtk_container_set_border_width(GTK_CONTAINER(vbox),5);
	gtk_container_add(GTK_CONTAINER(w),vbox);

	scrolled_w = gtk_scrolled_window_new(NULL,NULL);
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_w),GTK_POLICY_AUTOMATIC,
		GTK_POLICY_ALWAYS);
	gtk_box_pack_start(GTK_BOX(vbox),scrolled_w,TRUE,TRUE,0);

	clist = gtk_clist_new_with_titles(3,titles);
	gtk_signal_connect(GTK_OBJECT(clist),"select_row",G_CALLBACK(selection_made),NULL);
	gtk_clist_set_shadow_type(GTK_CLIST(clist),GTK_SHADOW_OUT);

	gtk_clist_set_column_width(GTK_CLIST(clist),0,150);

	gtk_container_add(GTK_CONTAINER(scrolled_w),clist);

	hbox = gtk_hbox_new(FALSE,0);
	gtk_box_pack_start(GTK_BOX(vbox),hbox,FALSE,TRUE,0);

	btn_add = gtk_button_new_with_label("Add Row");
	btn_clear = gtk_button_new_with_label("Clear List");
	btn_hide_show = gtk_button_new_with_label("Hide/Show titles");

	gtk_box_pack_start(GTK_BOX(hbox),btn_add,TRUE,TRUE,0);
	gtk_box_pack_start(GTK_BOX(hbox),btn_clear,TRUE,TRUE,0);
	gtk_box_pack_start(GTK_BOX(hbox),btn_hide_show,TRUE,TRUE,0);

	gtk_signal_connect_object(GTK_OBJECT(btn_add),"clicked",G_CALLBACK(button_add_clicked),
		clist);
	gtk_signal_connect_object(GTK_OBJECT(btn_clear),"clicked",G_CALLBACK(button_clear_clicked),
		clist);
	gtk_signal_connect_object(GTK_OBJECT(btn_hide_show),"clicked",
		G_CALLBACK(button_hide_show_clicked),clist);

	gtk_widget_show_all(w);
	gtk_main();
	return 0;
}